package cn.xz.zgsg.util;

import static android.content.ContentValues.TAG;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.xz.zgsg.base.BaseApplication;
import cn.xz.zgsg.bean.U3dListBean;
import cn.xz.zgsg.bean.U3dWelListBean;
import cn.xz.zgsg.net.OkHttpUtil;
import cn.xz.zgsg.net.OkhttpResponseHandle;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * 程序接口
 */
public class U3dHttp {

    //程序端口号-默认 8030，后根据需求修改
    private static final int mPort = 8030;

    //播放
    public static void PlayMovie(String ip) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=play&object=";
        OkHttpUtil.post(url);
    }

    //暂停
    public static void PauseMovie(String ip) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=pause&object=";
        OkHttpUtil.post(url);
    }

    //重播
    public static void ReplayMovie(String ip) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=replay&object=";
        OkHttpUtil.post(url);
    }

    //最大化/显示
    public static void MaxMovie(String ip) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=max&object=";
        OkHttpUtil.post(url);
    }

    //最小化/隐藏
    public static void MinMovie(String ip) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=min&object=";
        OkHttpUtil.post(url);
    }

    //上一页
    public static void PrevPage(String ip) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=prevPage&object=";
        OkHttpUtil.post(url);
    }

    //下一页
    public static void NextPage(String ip) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=nextPage&object=";
        OkHttpUtil.post(url);
    }

    //栏目点播
    public static void PostChannel(String ip, int tag) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=channel&object=" + tag;
        OkHttpUtil.post(url);
    }

    //内容点播
    public static void PostContent(String ip,int tag) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=content&object=" + tag;
        OkHttpUtil.post(url);
    }

    //返回主页
    public static void UnityHome(String ip) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=home&object=";
        OkHttpUtil.post(url);
    }

    //返回屏保
    public static void UnityScreen(String ip) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=screen&object=";
        OkHttpUtil.post(url);
    }

    //关闭弹窗
    public static void UnityClosePanel(String ip) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=ClosePanel&object=";
        OkHttpUtil.post(url);
    }


    //获取列表数据
    public interface GetU3dDataListener{
        void getData(U3dListBean mBean); //有参方法
    }
    public static void getU3dData(Context mContext, String ip, GetU3dDataListener mListener){
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=json&object=";
        OkHttpUtil.get(mContext, url, U3dListBean.class, new OkhttpResponseHandle<U3dListBean>() {
            @Override
            public void onSuccess(U3dListBean response) {
                if (response != null) {
                     mListener.getData(response);
                }
            }

            @Override
            public void onFailure(Request request, Exception e) {
                super.onFailure(request, e);
                Log.e(TAG, "程序接口:失败->" + "获取资源列表");
                mListener.getData(null);
            }
        });
    }

    //获取欢迎词
    public interface GetU3dWelListener{
        void getData(U3dWelListBean mBean); //有参方法
    }
    public static void getU3dWel(Context mContext, String ip, GetU3dWelListener mListener){
        String url = "http://" + ip + ":" + mPort + "/?act=GetWordJson";
        OkHttpUtil.get(mContext, url, U3dWelListBean.class, new OkhttpResponseHandle<U3dWelListBean>() {
            @Override
            public void onSuccess(U3dWelListBean response) {
                if (response != null) {
                    mListener.getData(response);
                }
            }

            @Override
            public void onFailure(Request request, Exception e) {
                super.onFailure(request, e);
                Log.e(TAG, "程序接口:失败->" + "获取资源列表");
                mListener.getData(null);
            }
        });
    }


    //播放对应欢迎词
    public static void SelectWel(String ip,int tag) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=StopLoop&object=" + tag;
        OkHttpUtil.post(url);
    }

    //欢迎词循环
    public static void LoopWel(String ip) {
        String url = "http://" + ip + ":" + mPort + "/?act=unity&states=AllLoop&object";
        OkHttpUtil.post(url);
    }

    public interface ChangeWelListener{
        void ok(); //有参方法
    }
    //修改欢迎词内容
    public static void ChangeWelContent(String ip,int tag, String s,ChangeWelListener changeWelListener){
        String url = "http://" + ip + ":" + mPort + "/?act=Text&index=" + tag + "&contain=" + s ;
        OkHttpUtil.get(url, new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                   changeWelListener.ok();
            }
        });
    }
    //修改欢迎词字体
    public static void ChangeWelFontContent(String ip,int tag, String s,ChangeWelListener changeWelListener){
        String url = "http://" + ip + ":" + mPort + "/?act=TextFont&index=" + tag + "&font=" + s ;
        OkHttpUtil.get(url, new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                changeWelListener.ok();
            }
        });
    }
    //修改欢迎词大小
    public static void ChangeWelSizeContent(String ip,int tag, int s,ChangeWelListener changeWelListener){
        String url = "http://" + ip + ":" + mPort + "/?act=TextSize&index=" + tag + "&size=" + s ;
        OkHttpUtil.get(url, new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                changeWelListener.ok();
            }
        });
    }



    //获取字体
    public interface GetU3FontListener{
        void getData(ArrayList<String> mList); //有参方法
    }
    public static void getU3Font(String ip, GetU3FontListener mListener){
        String url = "http://" + ip + ":" + mPort + "/?act=GetFontList";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()){
                    String responseString = response.body().string();
                    Gson gson = new Gson();
                    Type type = new TypeToken<List<String>>(){}.getType();
                    List<String> members = gson.fromJson(responseString, type);
                    mListener.getData((ArrayList<String>) members);
                }
            }
        });
    }

    //获取当前视频进度
    public interface GetMovieSizeListener{
        void getData(String maxTxt,String minTxt,int maxSeekBar,int seekProgress); //有参方法
    }
    public static void getMovieSize(String ip,GetMovieSizeListener mListener){
        String movieUrl =  "http://" + ip + ":" + mPort + "/?act=unity&states=progress&object=";
        OkHttpUtil.get(movieUrl, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG, "程序接口:失败->" + "获取视频进度");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String responseString = response.body().string();
                    //如果请求数据长度小于6直接不处理，服务端只要请求成功就会返回true，
                    // 这里截取前面字符串判断一下，要截取前几个，正常返回数据里也可能会有
                    if (responseString.length() < 6 || responseString.substring(0,5).contains("true")){
                        return;
                    }
                    String[] strs = responseString.split("\\|");
                    if (strs.length == 2) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                Log.e("str1", strs[0]);
                                Log.e("str1", strs[1]);
                                int a = 0;
                                int b = 0;
                                if (isNumeric(strs[0].substring(0, 1))) {
                                    if (isNumeric(strs[0])) {
                                        a = Integer.parseInt(strs[0]);
                                    } else {
                                        a = Double.valueOf(strs[0]).intValue();
                                    }
                                } else {
                                    if (isNumeric(strs[0])) {
                                        a = Integer.parseInt(strs[0]);
                                    } else {
                                        a = Double.valueOf(strs[0].substring(1, strs[0].length())).intValue();

                                    }

                                }
                                if (isNumeric(strs[1])) {
                                    b = Integer.parseInt(strs[1]);
                                } else {
                                    b = Double.valueOf(strs[1]).intValue();
                                }
                                int max = Math.max(a, b);
                                int min = Math.min(a, b);
                                mListener.getData(
                                        TimeUtils.getTime(Math.max(a, b)),
                                        TimeUtils.getTime(Math.min(a, b)),
                                        Math.max(a, b),
                                        Math.min(a, b)
                                );
                            }
                        }).start();
                    }
                }
            }
        });
    }


    //修改当前视频进度
    public static void changeMovie(String ip,int progress){
        OkHttpUtil.post("http://" + ip + ":" + mPort + "/?act=unity&states=seek&object=" + progress);
    }


    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }


    //获取创建基础分页列表数据
    public interface GetU3dCJFYDataListener{
        void getData(List<String> mBean); //有参方法
    }
    public static void getU3dCJFYData(Context mContext, String ip, GetU3dCJFYDataListener mListener){
        String url = "http://" + ip + ":" + mPort + "/?act=PageList";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                mListener.getData(new ArrayList<>());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()){
                    String responseString = response.body().string();
                    List<String> strings = Arrays.asList(responseString.split("[|]"));
                    mListener.getData(strings);
                }else {
                    mListener.getData(new ArrayList<>());
                }
            }
        });
    }


    //获取创建基础树列表数据
    public interface GetU3dCJDataListener{
        void getData(List<String> mBean); //有参方法
    }
    public static void getU3dCJData(Context mContext, String ip, GetU3dCJDataListener mListener){
        String url = "http://" + ip + ":" + mPort + "/?act=VideoList";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                mListener.getData(new ArrayList<>());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.isSuccessful()){
                    String responseString = response.body().string();
                    List<String> strings = Arrays.asList(responseString.split("[|]"));
                    mListener.getData(strings);
                }else {
                    mListener.getData(new ArrayList<>());
                }
            }
        });
    }

    //获取阶段成功下标
    public interface GetCGIndexListener{
        void getData(int mBean); //有参方法
    }
    public static void getCGIndex(String ip, GetCGIndexListener mListener){
        String url = "http://" + ip + ":" + mPort + "/?act=PageIndex";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    if(response.isSuccessful()){
                        String responseString = response.body().string();
                        String regEx="[^0-9]";
                        Pattern p = Pattern.compile(regEx);
                        Matcher m = p.matcher(responseString);
                        mListener.getData(Integer.parseInt( m.replaceAll("").trim()));
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }


}
